package com.cskaoyan.service;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author Austin Hsu
 * @Date 2022/9/8 16:19
 */

@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;


    @Override
    public CommonData<MarketStorage> query(MarketStorage marketStorage, BasePageInfo info) {   //xd分页 2022.9.8
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorageExample.setOrderByClause(info.getSort() + " " + info.getOrder());   //order by xxx desc/asc

        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        String key = marketStorage.getKey();
        String name = marketStorage.getName();
        if (key!=null && !"".equals(key)){criteria.andKeyLike("%"+key+"%");}           //where key like "%123%" and name like "%234%"
        if (name!=null && !"".equals(name)){criteria.andNameLike("%"+name+"%");}



        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);  //以这个语句查询

        PageInfo<MarketStorage> marketStoragePageInfo = new PageInfo<>(marketStorages);
        Integer total = (int) marketStoragePageInfo.getTotal();
        Integer pages = marketStoragePageInfo.getPages();              //这两个啥意思啊

        CommonData<MarketStorage> marketStorageCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), marketStorages);
        return marketStorageCommonData;

    }

    @Override
    public void delete(Integer id) {
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(id);

        marketStorageMapper.deleteByExample(example);
    }

    @Override
    public MarketStorage create(MultipartFile file) throws IOException {          //后端文件上传
        long size = file.getSize();                         //其实前端只要接收的是数值类型的都可以 不管是long还是int
        String name = file.getOriginalFilename();          //123.jpg
        String type = file.getContentType();
        Date updateTime = new Date();
        Date addTime = new Date();
        Boolean deleted = false;

        String str1 = name.substring(0, name.indexOf("."));    //截取第一个点之前的字符串 123.jpg
        String fileType = name.substring(str1.length()+1);     //eg:截取 jpg
        String key= UUID.randomUUID()+"."+fileType; //eg:bhjdsf.jpg

        String url="http://localhost:8083/wx/storage/fetch/"+key;      //一定要加http 不知道为啥！！！！
//----------------------------上面是返回给前端的数据-----------------------------
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setSize((int)size);
        marketStorage.setAddTime(addTime);
        marketStorage.setDeleted(deleted);
        marketStorage.setKey(key);
        marketStorage.setName(name);
        marketStorage.setType(type);
        marketStorage.setUpdateTime(updateTime);
        marketStorage.setUrl(url);
//---------------------------------文件保存-----------------------------------
        File myFile=new File("d:/spring/",key);       //按照新的文件名保存(按照原文件名 重复的会覆盖）
        file.transferTo(myFile);
//---------------------------------插入---------------------------------------
        marketStorageMapper.insertSelective(marketStorage);
        return marketStorage;
    }

    @Override
    public void update(MarketStorage marketStorage) {
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(marketStorage.getId());        //以id来插（唯一性）
        marketStorageMapper.updateByExampleSelective(marketStorage,example);
    }

    @Override
    public MarketStorage create2(MultipartFile file) throws IOException {             //微信文件上传
        long size = file.getSize();                         //其实前端只要接收的是数值类型的都可以 不管是long还是int
        String name = file.getOriginalFilename();          //123.jpg
        String type = file.getContentType();
        Date updateTime = new Date();
        Date addTime = new Date();
        Boolean deleted = false;

        String str1 = name.substring(0, name.indexOf("."));    //截取第一个点之前的字符串 123.jpg
        String fileType = name.substring(str1.length()+1);     //eg:截取 jpg
        String key= UUID.randomUUID()+"."+fileType; //eg:bhjdsf.jpg

        String url="http://localhost:8083/wx/storage/fetch/"+key;      //一定要加http 不知道为啥！！！！
//----------------------------上面是返回给前端的数据-----------------------------
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setSize((int)size);
        marketStorage.setAddTime(addTime);

        marketStorage.setKey(key);
        marketStorage.setName(name);
        marketStorage.setType(type);
        marketStorage.setUpdateTime(updateTime);
        marketStorage.setUrl(url);
//---------------------------------文件保存-----------------------------------
        File myFile=new File("d:/spring/",key);       //按照新的文件名保存(按照原文件名 重复的会覆盖）
        file.transferTo(myFile);
//---------------------------------插入---------------------------------------
        marketStorageMapper.insertSelective(marketStorage);
        return marketStorage;
    }
}
